<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@include file="/common/session.jsp" %>
<c:set var="noticeId" value="${param.noticeId}"/>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <LINK type="text/css" rel="stylesheet" href="${ctx}/css/default.css"/>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/MarketRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/NoticeRpcService.js'></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body>
<br>
<form id="noticeForm">
	<center>
        <input type="hidden" id="noticeId" name="noticeId" value="${noticeId}">
        <input type="hidden" id="userId" name="userId" value="${userId}">
        <table width="100%" cellpadding="5" cellspacing="5">
            <tr>
                <td colspan="2">
                    <input type="checkbox" style="width: 20px; height: 20px;" id="noticeTop" name="noticeTop">
                    <label for="noticeTop">置顶</label>
                </td>
                <td>
                    发送范围:
                </td>
                <td>
                    <select id="marketId" name="marketId">
			            <option value="">全部市场</option>
			        </select>
                </td>
                <td>
                    公告状态:
                </td>
                <td>
                    <select id="noticeState" name="noticeState">
			            <option value="1">&nbsp;&nbsp;有效&nbsp;&nbsp;</option>
			            <option value="0">&nbsp;&nbsp;无效&nbsp;&nbsp;</option>
			        </select>
                </td>
            </tr>
            <tr>
                <td>
                    开始时间:
                </td>
                <td>
                    <input type="date" id="noticeStartTime" name="noticeStartTime" />
                </td>
                <td>
                    截止时间:
                </td>
                <td>
                    <input type="date" id="noticeEndTime" name="noticeEndTime" />
                </td>
                <td>
                    公告标题:
                </td>
                <td>
                    <input type="text" placeholder="公告标题(必填 20字以内)" id="noticeTitle" name="noticeTitle"  maxlength="20" required/>
                </td>
            </tr>
            <tr>
            	<td>
                    公告内容:
                </td>
                <td colspan="5">
                    <textarea id="noticeContent" name="noticeContent" style=" height:160px;"></textarea>
                </td>
            </tr>
            <tr>
                <td style="border-bottom: 1px solid #e1ecf5" colspan="6">
                </td>
            </tr>
            <tr>
                <td colspan="6" align="center">
                    <button type="button" onclick="insertOrUpdateNotice()">保存</button>
                    <button type="button" onclick="callbackNotice()">返回</button>
                </td>
            </tr>
        </table>
    </center>
    
</form>
</body>
</html>

<script>

    //    显示市场列表
    initMarketSel();
    function initMarketSel(){
	    var market = new Market();
	    market.marketId = "${marketId}";
	    MarketRpcService.selectMarketList(market, callbackSelectMarketList);
    }

    function callbackSelectMarketList(list) {
        dwr.util.addOptions("marketId", list, "marketId", "marketName");
        dwr.util.setValue("marketId", "${marketId}");
        if ("${noticeId}".length > 0) {
            selectNoticeList();
        }
        else{
            setTime();
        }
    }

//    新增设置默认时间
    function setTime(){
        var startTime = new Date();
        var year = startTime.getFullYear() + 1;
        dwr.util.setValue("noticeStartTime", startTime.Format("yyyy-MM-dd"));
        dwr.util.setValue("noticeEndTime", year+startTime.Format("-MM-dd"));
    }

    function selectNoticeList() {
        var notice = new Notice();
        notice.noticeId = "${noticeId}";
        NoticeRpcService.selectNoticeList(notice, callbackSelectNoticeList);
    }

    function callbackSelectNoticeList(list) {
        dwr.util.setValues(list[0]);
        dwr.util.setValue("noticeStartTime", list[0].noticeStartTime.Format("yyyy-MM-dd"));
        dwr.util.setValue("noticeEndTime", list[0].noticeEndTime.Format("yyyy-MM-dd"));
        var noticeMarket = new NoticeMarket();
        noticeMarket.notice = list[0]
        noticeMarket.noticeId = "${noticeId}";
        NoticeRpcService.selectNoticeMarketList(noticeMarket, callbackSelectNoticeMarketList);
    }

    function callbackSelectNoticeMarketList(list) {
        if (list.length == 1) {
            dwr.util.setValue("marketId", list[0].marketId);
        }
    }

    function insertOrUpdateNotice() {
        if (!validate($("noticeForm"))) {
            return false;
        }
        var notice = dwr.util.getValues("noticeForm");
        var noticeStartTime = dwr.util.getValue("noticeStartTime");
        var noticeEndTime = dwr.util.getValue("noticeEndTime");
        notice.noticeStartTime = new Date(noticeStartTime);
        notice.noticeEndTime = new Date(noticeEndTime);
        var marketId = dwr.util.getValue("marketId");
        if ("${noticeId}".length > 0) {
            NoticeRpcService.updateNotice(notice,marketId, callbackNotice);
        } else {
            NoticeRpcService.insertNotice(notice,marketId, callbackNotice);
        }
    }

    function callbackNotice() {
        location = "${ctx}/cms/notice_list.jsp";
    }
</script>
